package com.hugang.juc.threadpool;

import java.util.concurrent.*;

/**
 * 线程池
 *
 * @author hg
 * @date 2020/8/21 10:47
 */
public class ThreadPoolDemo {

    public static void main(String[] args) throws InterruptedException {
        //定长的线程池，只有固定的线程数，不可伸缩线程数
//        ExecutorService pool = Executors.newFixedThreadPool(5);

        //单线程的线程池，只有一个线程，不可伸缩线程数
//        ExecutorService pool = Executors.newSingleThreadExecutor();

        //可伸缩线程数的线程，最大线程数为 Integer.MAX_VALUE
//        ExecutorService pool = Executors.newCachedThreadPool();

        //一般设置核心使用以下策略：
        //cpu密集型任务：核心线程数=cpu逻辑处理器数量 + 1
        //I/O密集型：核心线程数=cpu逻辑处理器数量 * 2
        //cpu密集型和io密集型参考：https://blog.csdn.net/weixin_40151613/article/details/81835974
//        System.out.println(Runtime.getRuntime().availableProcessors());
//        ExecutorService pool = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() + 1, Runtime.getRuntime().availableProcessors() * 2 + 2, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        ExecutorService pool = new ThreadPoolExecutor(5, 10, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
//        ExecutorService pool = new ThreadPoolExecutor(5, 10, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());
//        ExecutorService pool = new ThreadPoolExecutor(5, 10, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardPolicy());

        try {
            for (int i = 1; i < 20; i++) {
                int finalI = i;
                pool.execute(()->{
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " ---> "  + finalI);
                });
//                Future<Integer> submit = pool.submit(() -> {
//                    return 1;
//                });
//                System.out.println("=======> " + submit.get());

//                pool.submit(Runnable r, Object result)
//                Integer result = 1;
//                Future<?> submit = pool.submit(() -> {
//                    System.out.println(Thread.currentThread().getName() + " ---> " + finalI);
//                },result);
//                System.out.println("=======> " +submit.get());

//                try {
//                    TimeUnit.MILLISECONDS.sleep(10);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            pool.shutdown();
        }
        TimeUnit.SECONDS.sleep(5);
        System.out.println("main ---> over");
    }

}
